/**
* 基础菜单 系统管理 店铺管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>店铺管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!--表单-->
    <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm" style="margin: 40px 0px 0px 200px;">
      <el-form-item label="店铺名称" prop="dishes_name">
        <el-input size="small" v-model="editForm.dishes_name" auto-complete="off" style="width: 30%;"></el-input>
      </el-form-item>
      <el-form-item label="店铺地址" prop="deptNo">
        <el-input size="small" v-model="editForm.dishes_name" auto-complete="off" style="width: 30%;"></el-input>
      </el-form-item>
      <el-form-item label="食品安全档案" prop="deptNo">
        <el-input size="small" v-model="editForm.dishes_name" auto-complete="off" style="width: 30%;"></el-input>
      </el-form-item>
      <el-form-item label="商家青山档案" prop="deptNo">
        <el-input size="small" v-model="editForm.dishes_name" auto-complete="off" style="width: 30%;"></el-input>
      </el-form-item>
      <!-- <el-form-item label="Boss用户名" prop="deptNo">
        <el-input size="small" v-model="editForm.dishes_name" auto-complete="off" style="width: 30%;"></el-input>
      </el-form-item>
      <el-form-item label="Boss密码" prop="deptNo">
        <el-input size="small" type="password" v-model="editForm.dishes_name" auto-complete="off" style="width: 30%;"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="deptNo">
        <el-input size="small" type="password" v-model="editForm.dishes_name" auto-complete="off" style="width: 30%;"></el-input>
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer" style="margin: 40px 0px 0px 380px;">
      <el-button size="small" @click="closeDialog">取消</el-button>
      <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
    </div>
  </div>
</template>

<script>
  import {
    deptList,
    deptSave,
    deptDelete
  } from '../../api/userMG'
  import Pagination from '../../components/Pagination'
  export default {
    data() {
      return {
        category: [],
        show_sku: 1,
        nshow: true, //switch开启
        fshow: false, //switch关闭
        loading: false, //是显示加载
        editFormVisible: false, //控制编辑页面显示与隐藏
        title: '添加',
        editForm: {
          dishes_name:'',
          dishes_price: 0,
          category_name:'',
          dishes_desc:'',
          hotSell:'是',
          token: localStorage.getItem('logintoken')
        },
        // rules表单验证
        rules: {
          dishes_name: [{
            required: true,
            message: '请输入菜品名称',
            trigger: 'blur'
          }],
          dishes_price: [{
            required: true,
            message: '请输入菜品价格',
            trigger: 'blur'
          }],
          category_name: [{
            required: true,
            message: '请选择所属分类',
            trigger: 'blur'
          }],
          dishes_desc: [{
            required: true,
            message: '请输入菜品描述',
            trigger: 'blur'
          }]
        },
        formInline: {
          page: 1,
          limit: 10,
          varLable: '',
          varName: '',
          token: localStorage.getItem('logintoken')
        },
        // 删除部门
        seletedata: {
          ids: '',
          token: localStorage.getItem('logintoken')
        },
        userparm: [], //搜索权限
        listData: [], //用户数据
        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 10,
          total: 10
        }
      }
    },
    // 注册组件
    components: {
      Pagination
    },
    /**
     * 数据发生改变
     */

    /**
     * 创建完毕
     */
    created() {
      this.getdata(this.formInline);
      this.getCategory();
    },

    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      // 获取菜单列表
      getdata(parameter) {
        this.loading = true
        // 模拟数据开始
        let res = {
          code: 0,
          msg: null,
          count: 3,
          data: [{
              username: "测试001",
              add_time: 1521062371000,
              update_time: 1526700200000,
              dishes_id: 8,
              dishes_name:'冰淇淋',
              dishes_price: 10.5,
              category_name:'甜品',
              dishes_desc:'测试测试0001',
              dishes_monthly_sales:23,
              dishes_hot_sell:'是',
              dishes_new_product:'否',
              dishes_recommend:'是',
              parentId: 1
            },
            {
              username: '测试002',
              add_time: 1521063247000,
              update_time: 1526652291000,
              dishes_id: 2,
              dishes_name:'红烧肉',
              dishes_price: 1,
              category_name:'主食',
              dishes_desc:'测试测试0002',
              dishes_monthly_sales:24,
              dishes_hot_sell:'否',
              dishes_new_product:'否',
              dishes_recommend:'否',
              parentId: 1
            },
            {
              username: '测试003',
              add_time: 1526349555000,
              update_time: 1526349565000,
              dishes_id: 3,
              dishes_name:'青椒炒肉',
              dishes_price: 8,
              category_name:'主食',
              dishes_desc:'测试测试0003',
              dishes_monthly_sales:12,
              dishes_hot_sell:'否',
              dishes_new_product:'是',
              dishes_recommend:'是',
              parentId: 1
            }
          ]
        }
        this.loading = false
        this.listData = res.data
        this.pageparm.currentPage = this.formInline.page
        this.pageparm.pageSize = this.formInline.limit
        this.pageparm.total = res.count
        // 模拟数据结束

        /***
         * 调用接口，注释上面模拟数据 取消下面注释
         */
        // deptList(parameter)
        //   .then(res => {
        //     this.loading = false
        //     if (res.success == false) {
        //       this.$message({
        //         type: 'info',
        //         message: res.msg
        //       })
        //     } else {
        //       this.listData = res.data
        //       // 分页赋值
        //       this.pageparm.currentPage = this.formInline.page
        //       this.pageparm.pageSize = this.formInline.limit
        //       this.pageparm.total = res.count
        //     }
        //   })
        //   .catch(err => {
        //     this.loading = false
        //     this.$message.error('菜单加载失败，请稍后再试！')
        //   })
      },
      //获取分类
      getCategory() {
      	var that = this;
      	var arr = [
          {category_id:1,category_name:'主食'},
          {category_id:2,category_name:'甜品'}
        ];
        that.category = arr;
        if (this.category.length > 0) {
        	this.show_input = true
        }else{
        	this.$message({
        		showClose: true,
        		message: "请先创建商品分类",
        		type: "warning"
        	});
      	//获取所有分类
      	// this.http.get("category/admin/all_category").then(res => {
      	// 	that.category = res.data;
      	// 	if (this.category.length > 0) {
      	// 		this.show_input = true
      	// 	}else{
      	// 		this.$message({
      	// 			showClose: true,
      	// 			message: "请先创建商品分类",
      	// 			type: "warning"
      	// 		});
      	// 	}
      	// });
      }
      },
      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage
        this.formInline.limit = parm.pageSize
        this.getdata(this.formInline)
      },
      // 搜索事件
      search() {
        this.getdata(this.formInline)
      },
      //显示编辑界面
      handleEdit: function(index, row) {
        this.editFormVisible = true
        if (row != undefined && row != 'undefined') {
          this.title = '编辑'
          this.editForm = row
          // this.editForm.dishes_id = row.dishes_id
          // this.editForm.dishes_name = row.dishes_name
          // this.editForm.dishes_price = row.dishes_price
          // this.editForm.category_name = row.category_name
          // this.editForm.dishes_desc = row.dishes_desc
        } else {
          this.title = '添加'
          this.editForm = {}
          // this.editForm.dishes_id = ''
          // this.editForm.dishes_name = ''
          this.editForm.dishes_price = ''
          // this.editForm.category_name = ''
          // this.editForm.dishes_desc = ''
        }
      },
      // 编辑、增加页面保存方法
      submitForm(editData) {
        this.$refs[editData].validate(valid => {
          if (valid) {
            deptSave(this.editForm)
              .then(res => {
                this.editFormVisible = false
                this.loading = false
                if (res.success) {
                  this.getdata(this.formInline)
                  this.$message({
                    type: 'success',
                    message: '公司保存成功！'
                  })
                } else {
                  this.$message({
                    type: 'info',
                    message: res.msg
                  })
                }
              })
              .catch(err => {
                this.editFormVisible = false
                this.loading = false
                this.$message.error('公司保存失败，请稍后再试！')
              })
          } else {
            return false
          }
        })
      },
      // 删除公司
      deleteUser(index, row) {
        this.$confirm('确定要删除吗?', '信息', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(() => {
            deptDelete(row.deptId)
              .then(res => {
                if (res.success) {
                  this.$message({
                    type: 'success',
                    message: '公司已删除!'
                  })
                  this.getdata(this.formInline)
                } else {
                  this.$message({
                    type: 'info',
                    message: res.msg
                  })
                }
              })
              .catch(err => {
                this.loading = false
                this.$message.error('公司删除失败，请稍后再试！')
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      },
      // 关闭编辑、增加弹出框
      closeDialog() {
        this.editFormVisible = false
      }
    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }

  .userRole {
    width: 100%;
  }
</style>
